<?php
namespace Home\Controller;
use Think\Controller;
 
class IndexController extends Controller {

	// phone = 18939840579 pwd = 123456
	const AUTH = "bd58dbf876774a43c46d4f543d8326d4";

    public function index()
	{
       $this->display("login_v2");
    }

	
	public function loginHandle()
	{
		if(I("post.type") == 2) {
			$phone 	  = I("post.phone");
			$password = I("post.password");
			
			$hash = md5($phone.$password);
			if( $hash == self::AUTH) {
				session("adminID", 1);
				return $this->success("登陆成功", U("Admin/Index/index"));
			}else{
				return $this->error("账号或密码错误", U("Home/Index/index"));
			}
		}elseif(I("post.type") == 1) {
			$map["password"]     = MD5(C("PASSWORD_SALT").I("post.password"));
			$map["phone"]     	 = I("post.phone");		
			$userObj = M("user_hospital");
			$user = $userObj->where($map)->find();
			if(empty($user)) {
				return $this->error("账号或密码错误", U("Home/Index/index"));
			}
			session('doctorID',1);
			session('doctor_data', $user);
			return $this->success("登陆成功", U("/Console/Index/index"));
		}else {
			return $this->error("账号或密码错误", U( "Home/Index/index"));
		}
		
	}
	
	public function logout()
	{
		session("adminID", null);
		session("doctorID", null);
		session("doctor_data", null);
		return $this->redirect(U("/"));
	}
	
	private function _isMobile($mobile) 
	{
		if (!is_numeric($mobile)) {
			return false;
		}
		return preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#', $mobile) ? true : false;
	 }

}